package com.cd.servlet;

import cn.hutool.json.JSONUtil;
import com.cd.dao.BookDao;
import com.cd.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/book")
public class BookServlet extends HttpServlet {
	private BookDao bookDao;

	@Override
	public void init() throws ServletException {
		bookDao = new BookDao();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		List<Book> bookList = bookDao.findAll();
		String jsonStr = JSONUtil.toJsonStr(bookList);
		resp.getWriter().write(jsonStr);
		resp.getWriter().close();
	}
}
